﻿using AntdUI;
using System.Reflection.Metadata;
using WenAntdUI.Helps;
using Button = AntdUI.Button;

namespace WenAntdUI.Controls;

[ToolboxItem(true)]
public class UploadFile : IControl
{
    #region 私有属性
    private readonly Input input;
    private readonly Button button;
    private readonly Progress progress;
    private readonly Button upbutton;
    #endregion

    public UploadFile()
    {
        input = new Input()
        {
            Dock = DockStyle.Fill,
            JoinRight = true,
            BorderWidth = 1,
        };

        this.Controls.Add(input);


        button = new Button()
        {
            IconSvg = "SelectOutlined",
            BorderWidth = 1,
            Width = 20,
            Dock = DockStyle.Right,
            //Text = "选择",
            JoinLeft = true,
            JoinRight = true,
        };
        this.Controls.Add(button);

        progress = new Progress()
        {
            Padding = new Padding(5, 0, 5, 0),
            Dock = DockStyle.Fill,
            Height = 12,
            Margin = new Padding(0),
            Text = "上传进度",
            Visible = false,
        };
        this.Controls.Add(progress);

        upbutton = new Button()
        {
            IconSvg = "UploadOutlined",
            BorderWidth = 1,
            Width = 60,
            Dock = DockStyle.Right,
            Text = "上传",
            JoinLeft = true,
        };
        this.Controls.Add(upbutton);




        button.Click += Button_Click;
        upbutton.Click += Upbutton_Click;
    }

    private void Upbutton_Click(object? sender, EventArgs e)
    {

        if (!File.Exists(input.Text))
        {
            Msgbox.ShowError("文件不存在");
            return;
        }

        OnStart();
        UploadClick?.Invoke(this, input.Text);
    }

    public void OnStart()
    {
        input.Visible = false;
        button.Visible = false;
        upbutton.Loading = true;
        progress.Value = 0;
        progress.Visible = true;
    }

    public void OnEnd()
    {
        input.Visible = true;
        button.Visible = true;
        upbutton.Loading = false;
        progress.Visible = false;
    }

    #region 公有属性

    [Category("Wem"), Description("上传进度"), DefaultValue(0)]
    public float ProgressValue
    {
        get => progress.Value;
        set
        {
            if (value == 1)
            {
                this.Invoke(OnEnd);
            }
            progress.Value = value;
        }
    }

    [Category("Wem"), Description("文件筛选"), DefaultValue("全部文件|*.*|图档|*.png;*.jpg;*.jpeg;*.bmp;*.ico|压缩文件|*.zip;*.rar;*.7z|音频|*.mp3;*.wav|视频|*.mp4;*.avi|PDF文档|*.pdf|CAD位图|*.dwg;*.dxf|办公|*.xls;*.xlsx;*.doc;*.docx;*.ppt;*.pptx")]
    public string Filter { get; set; } = "全部文件|*.*|图档|*.png;*.jpg;*.jpeg;*.bmp;*.ico|压缩文件|*.zip;*.rar;*.7z|音频|*.mp3;*.wav|视频|*.mp4;*.avi|PDF文档|*.pdf|CAD位图|*.dwg;*.dxf|办公|*.xls;*.xlsx;*.doc;*.docx;*.ppt;*.pptx";
    #endregion


    private void Button_Click(object? sender, EventArgs e)
    {
        var dia = new OpenFileDialog()
        {
            Filter = Filter
        };

        if (dia.ShowDialog() == DialogResult.OK)
        {
            var filename = dia.FileName;
            this.input.Text = filename;
        }
    }
    public event EventHandler<string> UploadClick;
}

